# -*- coding: utf-8 -*-
# @File     : make_secrit.py
# @Author   : hewenjun
# @Desc     : 描述
import hashlib
import time

PRIVATE_KEY = 'Gy6DIF&FnGTO%3gc'
time_st = round(time.time() * 1000)


def make_stored_str(req_data):
    if isinstance(req_data, list):
        for item in req_data:
            make_stored_str(item)
    elif isinstance(req_data, dict):
        for key in sorted(req_data.keys()):
            make_stored_str(req_data[key])
    else:
        global value
        value += str(req_data)
    return value


def get_sign(req_data, tk):
    # req_data = {
    #     "abacusCouponDTOS": coup_list,
    #     "agreePurchaseNotes": 0,
    #     "invoiceDTO": {
    #         "invoiceCatalog": 0,
    #         "invoiceType": 0,
    #         "makeOutInvoice": 1
    #     },
    #     "payType": "wxpay",
    #     "preOrderId": pre_id,
    #     "receiverId": adress_id,
    #     "shopOrderDTOS": [{
    #         "shopId": 1,
    #         "skuDTOList": [{
    #             "sku": sku,
    #             "skuNum": 1
    #         }]
    #     }],
    #     "skuTotalMultiplexPrice": 0,
    #     "skuTotalPlatformPrice": 2,
    #     "skuTotalShippingPrice": 0,
    #     "skuTotalShopPromotionPrice": 0,
    #     "subTotalPrice": 0,
    # }
    sign_str = make_stored_str(req_data)
    # print("排序后的字符串", sign_str)
    full_str = hashlib.md5(sign_str.encode(encoding='UTF-8')).hexdigest()
    # print("第一次转md5", full_str)
    format_str = full_str + ":" + tk + ":" + str(time_st) + ":" + PRIVATE_KEY
    # print(format_str)
    signs = hashlib.md5(format_str.encode(encoding='UTF-8')).hexdigest()
    # print("sign值", signs)
    return signs
